<?php
namespace App\Http\Controllers\Admin\Ywmall;

use App\Service\Admin\Ywmall\ShopService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class ShopController extends BaseController
{
    //物品

    //列表
    public function index(Request $request)
    {
        $res = ShopService::index();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $img = $request->post('img', '');
        if ($img) {
            if (substr($img, 0, 5) == 'data:') {
                $pas['head'] = Upload::getPathByBase64($img);
            } else {
                $pas['head'] = $img;
            }
        }
        if ($id) {
            $res = ShopService::modify($id, $pas);
        } else {
            $res = ShopService::add($pas);
        }
        if ($res['code']!=0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = ShopService::del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}